package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.system.domain.Articles;
import com.ruoyi.system.domain.Videos;
import com.ruoyi.system.mapper.ArticlesMapper;
import com.ruoyi.system.mapper.VideosMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.HistoryMapper;
import com.ruoyi.system.domain.History;
import com.ruoyi.system.service.IHistoryService;

import javax.annotation.Resource;

/**
 * 浏览历史Service业务层处理
 *
 * @author AousVolad
 * @date 2024-06-27
 */
@Service
public class HistoryServiceImpl implements IHistoryService {
    @Autowired
    private HistoryMapper historyMapper;

    @Resource
    private ArticlesMapper articlesMapper;

    @Resource
    private VideosMapper videosMapper;

    /**
     * 查询浏览历史
     *
     * @param historyId 浏览历史主键
     * @return 浏览历史
     */
    @Override
    public History selectHistoryByHistoryId(Long historyId) {
        return historyMapper.selectHistoryByHistoryId(historyId);
    }

    /**
     * 查询浏览历史列表
     *
     * @param history 浏览历史
     * @return 浏览历史
     */
    @Override
    public List<History> selectHistoryList(History history) {
        return historyMapper.selectHistoryList(history);
    }

    /**
     * 新增浏览历史
     *
     * @param history 浏览历史
     * @return 结果
     */
    @Override
    public int insertHistory(History history) {
        if(history.getArticlesId()!=null){
            Articles articles = articlesMapper.selectArticlesById(history.getArticlesId());
            articles.setViews(articles.getViews()+1);
            articlesMapper.updateArticles(articles);
        }else if(history.getVideosId()!=null){
            Videos videos = videosMapper.selectVideosById(history.getVideosId());
            videos.setViews(videos.getViews()+1);
            videosMapper.updateVideos(videos);
        }
        return historyMapper.insertHistory(history);
    }

    /**
     * 修改浏览历史
     *
     * @param history 浏览历史
     * @return 结果
     */
    @Override
    public int updateHistory(History history) {
        return historyMapper.updateHistory(history);
    }

    /**
     * 批量删除浏览历史
     *
     * @param historyIds 需要删除的浏览历史主键
     * @return 结果
     */
    @Override
    public int deleteHistoryByHistoryIds(Long[] historyIds) {
        return historyMapper.deleteHistoryByHistoryIds(historyIds);
    }

    /**
     * 删除浏览历史信息
     *
     * @param historyId 浏览历史主键
     * @return 结果
     */
    @Override
    public int deleteHistoryByHistoryId(Long historyId) {
        return historyMapper.deleteHistoryByHistoryId(historyId);
    }
}
